<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;
use app\api\common\Program;
use think\Config;
use think\Db;
use think\Validate;

use app\common\model\Brand as BrandModel;
use app\common\model\Goods as GoodsModel;

/**
 * 品牌接口
 */
class Brand extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取品牌列表
     */
    public function getBrand()
    {
        $data = BrandModel::field("id, name, image")->where("status", "=", "1")->order("weigh desc")->select();
        
        $this->success('请求成功',$data);
    }

    /**
     * 获取品牌详情
     */
    public function getBrandInfo()
    {
        // 接收参数
        $id = $this->request->post("id/d", 0);

        if(empty($id)){
            $this->error('数据不存在');
        }

        $data = BrandModel::where("id", "=", $id)->find();
        
        $this->success('请求成功',$data);
    }

    /**
     * 获取品牌产品分类
     */
    public function getTypeList()
    {
        // 接收参数
        $brand_id = $this->request->post("brand_id/d", 0);

        // 查询条件
        $where = [
            "gds.brand_id" => $brand_id,
            "gds.status" => "1",
        ];

        $data = GoodsModel::alias("gds")
            ->join("typeone tpe", "tpe.id = gds.one_id", "left")
            ->field("gds.one_id, tpe.image, tpe.name, tpe.id")
            ->where($where)
            ->group("gds.one_id")
            ->order("tpe.weigh desc")
            ->select();
            // dump(GoodsModel::alias("gds")->getLastSql());
            // die;
        
        $this->success('请求成功',$data);
    }
}
